import QtQuick 1.0
//import ShellInvoker 1.0
import "../../../common/lib/kdeadmouseutils-build-desktop"

Item {
  //height : 700
  //width : 1024
  property alias mediaType : wget.mediaType
  property alias codeMedia : wget.codeMedia
  property alias wget : wget
  signal retour();
  focus:true
  Keys.onPressed: {
      console.log('qml pressed:' + event.key);
      if (event.key == Qt.Key_Backspace) {
	  retour();
      }else if (event.key == Qt.Key_Space) {
	  if(backgroundVideo.isPlaying())
	    backgroundVideo.pause();
	  else if(backgroundVideo.isPaused())
	    backgroundVideo.play();
      }else if (event.key == Qt.Key_D) {
	backgroundVideo.reload();
      }
      
      /*
      if (event.key == Qt.Key_Return) {
	  appControl.videoBackground=true;
	  backgroundVideo.stop();
	  wget.mediaType='direct';
	  wget.codeMedia=setting.value('magikPrefix')+'paulscha.php?films=Barbie%20Fairytopia%20%282005%29%20Dvdrip.avi'
	  wget.startDwnld();
      }else if (event.key == Qt.Key_R) {
	  appControl.videoBackground=true;
	  backgroundVideo.stop();
	  wget.mediaType='direct';
	  wget.codeMedia=setting.value('magikPrefix')+'paulscha.php?films=Barbie%20Lac%20des%20signes%20%282003%29%20Dvdrip.avi'
	  wget.startDwnld();
      }else if (event.key == Qt.Key_Y) {
	  appControl.videoBackground=true;
	  backgroundVideo.stop();
	  wget.mediaType='youtube';
	  wget.codeMedia='3STgfrutcnI'
	  wget.startDwnld();
      }else if (event.key == Qt.Key_S) {
	backgroundVideo.play();
	  
      }*/
      event.accepted=true;
  }
  Connections {
      target: backgroundVideo
      onPosition : {
	//if (playRect.actif)
	  playRect.pos_ms=time_ms;
	//console.log('==========>new posit:'+time_ms);
      }
      
      onTotalTimeChanged : {
	playRect.total_ms=time_ms;
	console.log('==========>new total:'+time_ms);
      }
      //onSeekableChanged : console.log('==========>new seekable:'+isSeekable);
  }
  Connections {
      target: backgroundVideo
      onPlayStateRaise : {
	console.log('===>playin..:');
	actImg.state='pause';
      }
      onPauseStateRaise : {
	console.log('===>playin..:');
	actImg.state='play';
      }
      //onSeekableChanged : console.log('==========>new seekable:'+isSeekable);
  }
  
  Image {
	id:actImg
	fillMode: Image.PreserveAspectFit
	anchors.horizontalCenter:parent.horizontalCenter
	anchors.bottom:mainRect.top
	anchors.bottomMargin : 20
	width:50
	height:50
	smooth: true
	focus:true
	property string state 
	source:'../../../common/images/'+state+'.png'
  }
  Rectangle {
      id:mainRect
      property int totalTime
      property int m_pos
      height: 10
      width: parent.width - 300
      anchors.horizontalCenter: parent.horizontalCenter
      anchors.bottomMargin : 30
      anchors.bottom:parent.bottom
      color:'#FFFFFF'
      radius: 5
      smooth:true
      
      QmlSetting{
	id:setting
	appName:'allocine'
      }
      //outils de téléchargement:
      WgetProgress{
	id:wget
	//codeMedia : setting.value('magikPrefix')+'paulscha.php?films=Catch%20.44%20%2801-12%29%20ActDraWes%20DvdRip%20%23182451%23%20%24tt1886493%24.avi'
	//mediaType : 'direct'
	onAlldone : {
	  /*if(backgroundVideo.isPlaying())
	    backgroundVideo.reload();
	  else
	    backgroundVideo.playlocalfile(wget.location);*/
	  console.log('==========>alldone new total:' + backgroundVideo.totalTime());
	  playRect.actif=true;
	}
	onStartToDwnld : backgroundVideo.playlocalfile(wget.location);
      }
      
      //avancement du téléchargement
      Rectangle {
	id:dwnlRect
	property int i : wget.progress
	height: parent.height
	radius:parent.radius
	width:i*parent.width/100;
	color:'#AA9D91'
	smooth:true
      }
      
      //avancement de la lecture
      Rectangle {
	id:playRect
	property bool actif : false
	property int total_ms : 1
	property int pos_ms : 0
	height: parent.height
	radius:parent.radius
	width: pos_ms*parent.width/total_ms;
	color:'#91AA9D'
	smooth:true
      }
      
      
}
}